package com.evzgaga.evzrecon.util;

/**
 *
 * @author Bastien
 */
public class LittleEndianConverter {

    public static byte[] toLittleEndian(int number) {
        byte[] convertedValue = new byte[4];
        
        convertedValue[0] = (byte)(number & 0xFF);
        convertedValue[1] = (byte)((number >> 8) & 0xFF);
        convertedValue[2] = (byte)((number >> 16) & 0xFF);
        convertedValue[3] = (byte)((number >> 24) & 0xFF);
                
        return convertedValue;
    }
    
    public static int fromLittleEndian(byte[] byteData) {
        short[] data = signedToUnsigned(byteData);
        return (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
    }

    public static short[] signedToUnsigned(byte[] byteData) {
        short[] unsignedata = new short[byteData.length];
        
        for (int i = 0; i < byteData.length; i++) {
            unsignedata[i] = (short) (byteData[i] & 0xFF);
        }
        return unsignedata;
    }
}
